package com.yx.backstage.config.global;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一结果返回
 * @author YangXiong
 */
@Data
public class CommonResult<T> implements Serializable {
    private static final Long serialVersionUID = 1L;
    @ApiModelProperty(value = "状态码")
    private String code;
    @ApiModelProperty(value = "记录总数")
    private Long total;
    @ApiModelProperty(value = "数据")
    private T data;

    @ApiModelProperty(value = "响应信息")
    private String message;

    public CommonResult (String message) {
        this.code = "403";
        this.message = message;
    }

    public CommonResult() {
    }

    public static <T> CommonResult<T> success(T data) {
        CommonResult<T> response = new CommonResult<>();
        response.setCode("200");
        response.setMessage("true");
        response.setData(data);
        return response;
    }

    public static <T> CommonResult<T> success(T data, Long total) {
        CommonResult<T> rb = new CommonResult<>();
        rb.setCode("200");
        rb.setMessage("true");
        rb.setTotal(total);
        rb.setData(data);
        return rb;
    }

    public CommonResult<T> success(T data, String message) {
        CommonResult<T> rb = new CommonResult<>();
        rb.setCode("200");
        rb.setMessage(message);
        rb.setTotal(null);
        rb.setData(data);
        return rb;
    }

    public static <T> CommonResult<T> fail(String message) {
        CommonResult<T> response = new CommonResult<>();
        response.setCode("500");
        response.setMessage(message);
        response.setData(null);
        return response;
    }
}
